package red.internal.server.registry.handler;

import io.netty.channel.Channel;
import red.internal.core.handler.SenderListener;
import red.internal.core.message.RegistryMessage;
import red.internal.server.registry.storage.RegistryStorage;

/**
 * @author Jin Zheng
 * @since 1.0 2019-07-11
 */
public class UnbindRegistryHandler implements RegistryHandler
{
	private final RegistryStorage storage;

	public UnbindRegistryHandler(RegistryStorage storage)
	{
		this.storage = storage;
	}

	@Override
	public void handle(RegistryMessage message, Channel channel)
	{
		storage.disconnect(message.getItem(), channel);
		RegistryMessage response = message.success();
		channel.writeAndFlush(response).addListener(new SenderListener(response));
	}
}
